home *** CD-ROM | disk | FTP | other *** search
/ s-gikan2.maizuru-ct.ac.jp / s-gikan2.maizuru-ct.ac.jp.zip / s-gikan2.maizuru-ct.ac.jp / pub / ncvc352a_install.exe / {app} / scripts / divide_circular.pl < prev    next >
Perl Script  |  2007-10-23  |  7KB  |  276 lines

  1. #! /usr/bin/perl
  2.  
  3. #  ë~î╩òΓè╘é≡Å█î└é▓é╞é╔ò¬èäé╖éΘâXâNâèâvâg  #
  4. #  ÉΓæ╬ì└òWîn(G90)é╠é▌æ╬ë₧                 #
  5.  
  6. ####èeÄφÉ▌ÆΦ################################
  7. # ì└òWé≡żÉöô_ò\Īé╖éΘÅΩìçé═ 0
  8. # 1/1000ò\Īé╖éΘÅΩìçé═ 1
  9. # é≡É▌ÆΦé╡é─é¡é╛é│éóüB
  10. $coordinateNotation= 1;
  11. ############################################
  12.  
  13. use Math::Trig;
  14. $EPSILON = 0.0001;
  15.  
  16. $pre_file= $ARGV[0];
  17. $out_file= $ARGV[1];
  18.  
  19. open(IN,$pre_file);
  20. @line = <IN>;
  21. close(IN);
  22.  
  23. open(OUT,">$out_file");
  24.  
  25. my ($line,$i,$j);
  26. my ($G0X,$radius);
  27. my @point;
  28. my @currentIJ;
  29.  
  30. my ($x,$y);    #ê┌ô«æOé╠ì└òWé≡èiö[
  31. my @abso;    #ê┌ô«îπé╠ì└òW
  32. my @destination;    #ê┌ô«æOé╠ì└òWé⌐éτê┌ô«îπé╠ì└òWé▄é┼é╠æèæ╬ì└òW
  33. my @center;    #ê┌ô«æOé╠ì└òWé⌐éτë~î╩é╠ÆåÉSé▄é┼é╠æèæ╬ì└òW
  34. my @passAx;    #ë~î╩òΓè╘Ä₧é╔é▄é╜é«Ä▓é╠âèâXâg
  35.  
  36. %XYZ = ("X",0,"Y",1,"Z",2,"I",0,"J",1);
  37.  
  38. for($i=0;$i<=$#line;$i++){
  39.     $_ = $line[$i];
  40.     
  41.     if(/^\s*[\(\%]/){ print OUT; }
  42.     else{
  43.         if(/G0*?([0123])[A-Z\s]/){ $G0X = $1; }
  44.         elsif(/G8[0-9][A-Z\s]/){ $G0X = 9; }
  45.         
  46.         if($G0X =~ /[23]/ and /[XYIJ][^A-Z\s]+/){
  47.             ($x,$y) = ($abso[0],$abso[1]);
  48.             ($center[0],$center[1]) = (0,0);
  49.             ($destination[0],$destination[1]) = (0,0);
  50.             
  51.             while(/([XY])([^A-Z\s]+)/g){
  52.                 $destination[$XYZ{$1}] = $2 - $abso[$XYZ{$1}];
  53.                 $abso[$XYZ{$1}]=$2;
  54.             }
  55.             
  56.             if(/R([^A-Z\s]+)/){
  57.                 $radius = $1;
  58.                 @center = getCenter($destination[0],$destination[1],$radius,$G0X);
  59.                 
  60.                 $sAngle = getAngle(-$center[0],-$center[1]);
  61.                 $eAngle = getAngle($destination[0]-$center[0],$destination[1]-$center[1]);
  62.                 @passAx = getPassAxis($sAngle,$eAngle,$G0X);
  63.                 
  64.                 if($#passAx>=0){
  65.                     if($radius<0){ $radius = marume(abs($radius)); }
  66.                     @point = getIntersectionPoint($passAx[0],$x,$y,$radius,@center);
  67.                     while(/G[^A-Z\s]+/g){
  68.                         print OUT $&;
  69.                     }
  70.                     print OUT "X".$point[0];
  71.                     print OUT "Y".$point[1];
  72.                     print OUT "R".$radius;
  73.                     while(/[A-FHK-QS-VZ][^A-Z\s]+/g){
  74.                         print OUT $&;
  75.                     }
  76.                     print OUT "\n";
  77.                     
  78.                     for($j=1; $j<=$#passAx; $j++){
  79.                         @point = getIntersectionPoint($passAx[$j],$x,$y,$radius,@center);
  80.                         print OUT "X".$point[0];
  81.                         print OUT "Y".$point[1];
  82.                         print OUT "R".$radius."\n";
  83.                     }
  84.                     
  85.                     print OUT "X".$abso[0]."Y".$abso[1]."R".$radius."\n";
  86.                 }
  87.                 else{
  88.                     print OUT;
  89.                 }
  90.             }
  91.             else{
  92.                 while(/([IJ])([^A-Z\s]+)/g){
  93.                     $center[$XYZ{$1}] = $2;
  94.                 }
  95.                 $radius = getRadius($center[0],$center[1]);
  96.                 $sAngle = getAngle(-$center[0],-$center[1]);
  97.                 $eAngle = getAngle($destination[0]-$center[0],$destination[1]-$center[1]);
  98.                 @passAx = getPassAxis($sAngle,$eAngle,$G0X);
  99.                 
  100.                 if($#passAx>=0){
  101.                     @point = getIntersectionPoint($passAx[0],$x,$y,$radius,@center);
  102.                     while(/G[^A-Z\s]+/g){
  103.                         print OUT $&;
  104.                     }
  105.                     print OUT "X".$point[0];
  106.                     print OUT "Y".$point[1];
  107.                     while(/[A-FH-VZ][^A-Z\s]+/g){
  108.                         print OUT $&;
  109.                     }
  110.                     print OUT "\n";
  111.                     
  112.                     for($j=1; $j<=$#passAx; $j++){
  113.                         @point = getIntersectionPoint($passAx[$j],$x,$y,$radius,@center);
  114.                         print OUT "X".$point[0];
  115.                         print OUT "Y".$point[1];
  116.                         @currentIJ = getCurrentIJ($passAx[$j-1],$radius);
  117.                         if($currentIJ[0] != 0){ print OUT "I".$currentIJ[0]; }
  118.                         if($currentIJ[1] != 0){ print OUT "J".$currentIJ[1]; }
  119.                         print OUT "\n";
  120.                     }
  121.                     print OUT "X".$abso[0]."Y".$abso[1];
  122.                     @currentIJ = getCurrentIJ($passAx[$#passAx],$radius);
  123.                     if($currentIJ[0] != 0){ print OUT "I".$currentIJ[0]; }
  124.                     if($currentIJ[1] != 0){ print OUT "J".$currentIJ[1]; }
  125.                     print OUT "\n";
  126.                 }
  127.                 else{
  128.                     print OUT;
  129.                 }
  130.             }
  131. #print "G0X:$G0X, R:$radius\n";
  132. #print "center:@center\n";
  133. #print "@passAx\n\n";
  134.  
  135.         }
  136.         else{
  137.             print OUT;
  138.             while(/([XYZ])([^A-Z\s]+)/g){
  139.                 $abso[$XYZ{$1}] = $2;
  140.             }
  141.         }
  142.     }
  143. }
  144.  
  145. close(OUT);
  146.  
  147.  
  148. sub marume{
  149.     my ($num) = @_;
  150.  
  151.     if($coordinateNotation == 1){
  152.         return int($num+0.5);
  153.     }
  154.     else{
  155.         $num = int($num * 1000);
  156.         $num = $num / 1000;
  157.         if($num !~ /\./ and $num != 0){ $num = $num.'.'; }
  158.         return $num;
  159.     }
  160. }
  161.  
  162. sub getRadius{
  163.     my ($dx,$dy) = @_;
  164.     
  165.     return marume(sqrt($dx*$dx+$dy*$dy));
  166. }
  167.  
  168. sub getCenter{
  169.     my ($dx,$dy,$r,$g0x) = @_;
  170.     my ($fugou,$f1,$f2);
  171.     my (@center);
  172.     
  173.     $dx = int(($dx) * 1000);
  174.     $dy = int(($dy) * 1000);
  175.     $r = int($r * 1000);
  176.     
  177.     my $obj = sqrt(4*$r*$r/($dx*$dx+$dy*$dy) - 1);
  178.     if($r > 0){ $f1 = 1; }
  179.     else{ $f1 = -1; }
  180.     if($g0x == 2){ $f2 = 1; }
  181.     else{ $f2 = -1; }
  182.     $fugou = $f1 * $f2;
  183.     
  184.     $center[0] = ($dx + $fugou*$dy*$obj)/2000;
  185.     $center[0] = marume($center[0]);
  186.     $center[1] = ($dy - $fugou*$dx*$obj)/2000;
  187.     $center[1] = marume($center[1]);
  188.     return @center;
  189. }
  190.  
  191. sub getAngle{
  192.     my ($x,$y) = @_;
  193.     my $theta = atan2($y,$x);
  194.     if($theta < 0){ $theta += 2*pi; }
  195.     return $theta;
  196. }
  197.  
  198.  
  199. sub getPassAxis{
  200.     my ($s,$e,$g) = @_;
  201.     my ($ax,@passAxisList);
  202.     # @passAxisList:
  203.     #  0: +xÄ▓
  204.     #  1: +yÄ▓
  205.     #  2: -xÄ▓
  206.     #  3: -yÄ▓
  207.     
  208.     # èJÄnô_üAÅIù╣ô_é╠Å█î└é≡ĵô╛
  209.     #    0 <= Theta <  90: 1
  210.     #     90 <= Theta < 180: 2
  211.     #    180 <= Theta < 270: 3
  212.     #  270 <= Theta < 360: 4
  213.     my $sQuadrant = int($s / (pi/2)) + 1;
  214.     my $eQuadrant = int($e / (pi/2)) + 1;
  215.     
  216.     if($g == 2){    # Ä₧îvë±éΦ
  217.         if($sQuadrant == $eQuadrant and $s > $e){ return; }    # Ä▓é≡é▄é╜é¬é╚éóÅΩìç
  218.         if($sQuadrant <= $eQuadrant){ $sQuadrant += 4;}    # æµ1Å█î└é⌐éτæµ4Å█î└é╠é╞é½(+xÄ▓é≡é▄é╜é«ÅΩìç)
  219.         
  220.         #èJÄnô_é¬Ä▓Åπé╠ÅΩìçé═Ä▓é≡é▄é╜é¬é╚éóé╠é┼
  221.         if(abs(sin($s*2)) < $EPSILON){ $sQuadrant -= 1; }
  222.         
  223.         for($ax=$sQuadrant; $ax>$eQuadrant; $ax--){
  224.             push(@passAxisList,($ax-1)%4);    # Ä₧îvë±éΦé╚é╠é┼(Å█î└-1)%4é╠Ä▓é≡Æ╩ë▀é╖éΘ
  225.         }
  226.     }
  227.     else{    # ö╜Ä₧îvë±éΦ
  228.         if($sQuadrant == $eQuadrant and $s < $e){ return; }    # Ä▓é≡é▄é╜é¬é╚éóÅΩìç
  229.         if($sQuadrant >= $eQuadrant){ $eQuadrant += 4;}    # æµ4Å█î└é⌐éτæµ1Å█î└é╠é╞é½(+xÄ▓é≡é▄é╜é«ÅΩìç)
  230.         
  231.         #ÅIù╣ô_é¬Ä▓Åπé╠ô_é═Ä▓é≡é▄é╜é¬é╚éóé╠é┼
  232.         if(abs(sin($e*2)) < $EPSILON){ $eQuadrant -= 1; }
  233.         
  234.         for($ax=$sQuadrant; $ax<$eQuadrant; $ax++){
  235.             push(@passAxisList,($ax)%4);    # ö╜Ä₧îvë±éΦé╚é╠é┼(Å█î└)%4é╠Ä▓é≡Æ╩ë▀é╖éΘ
  236.         }
  237.     }
  238.     return @passAxisList;
  239. }
  240.  
  241. sub getIntersectionPoint{
  242.     my ($ax,$x,$y,$radius,@center) = @_;
  243.     # $Ax
  244.     #  0: +xÄ▓
  245.     #  1: +yÄ▓
  246.     #  2: -xÄ▓
  247.     #  3: -yÄ▓
  248.     
  249.     my @xAx = (1,0,-1,0);
  250.     my @yAx = (0,1,0,-1);
  251.     my @ip;
  252.     
  253.     $ip[0] = marume($x + $center[0] + $radius*$xAx[$ax]);
  254.     $ip[1] = marume($y + $center[1] + $radius*$yAx[$ax]);
  255.     
  256.     return @ip;
  257. }
  258.  
  259. sub getCurrentIJ{
  260.     my ($ax,$radius) = @_;
  261.     # $Ax
  262.     #  0: +xÄ▓
  263.     #  1: +yÄ▓
  264.     #  2: -xÄ▓
  265.     #  3: -yÄ▓
  266.     
  267.     my @iAx = (-1,0,1,0);
  268.     my @jAx = (0,-1,0,1);
  269.     my @ij;
  270.     
  271.     $ij[0] = marume($radius * $iAx[$ax]);
  272.     $ij[1] = marume($radius * $jAx[$ax]);
  273.  
  274.     return @ij;
  275. }
  276.